﻿using Cofe.Core.Interfaces;
using Cofe.Core.Logger;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Cofe.Core.Logger
{
    public class DummyCofeSystemEventSource : IManualCofeSystemEventSource
    {
        public DummyCofeSystemEventSource()
        {
            CancellationToken = _cts.Token;
            _cts.Cancel();
        }
        private CancellationTokenSource _cts = new CancellationTokenSource();
        public void NotifyCreated(string fullParsePath, bool isDirectory) { }        
        public void NotifyChanged(string fullParsePath, bool isDirectory) { }
        public void NotifyDeleted(string fullParsePath, bool isDirectory) { }
        public void NotifyRenamed(string fullParsePath, string oldParsePath, bool isDirectory) { }
        public bool IsEventEnabled { get; set; }
        public event EventHandler<CofeSystemEventArgs> OnEvent;

        public System.Threading.CancellationToken CancellationToken { get; private set; }
        public void Dispose() { }


        public IEnumerable<Type> SupportedEventType
        {
            get { return new List<Type>() { typeof(CofeSystemEventArgs) }; }
        }
    }

    public class ManualCofeSystemEventSource : EventSource<CofeSystemEventArgs>, IManualCofeSystemEventSource
    {
        #region Constructor

        public ManualCofeSystemEventSource(IVolumeInfo volume)
        {
            _volumeId = volume.VolumeId;
        }

        #endregion

        #region Methods

        private string checkFullParsePath(string fullParsePath)
        {
            string key, path;
            if (!(PathFE.ParseFullParseName(fullParsePath, out key, out path)))
                throw new ArgumentException("Not a full parse path.");
            if (key != _volumeId)
                throw new ArgumentException("VolumeId is different.");
            return fullParsePath;
        }

        public void NotifyCreated(string fullParsePath, bool isDirectory)
        {
            dispatchEvent(CofeSystemEventArgs.FromParsePath(checkFullParsePath(fullParsePath), 
                isDirectory, Core.ModifiedAction.Created));
        }

        public void NotifyChanged(string fullParsePath, bool isDirectory)
        {
            dispatchEvent(CofeSystemEventArgs.FromParsePath(checkFullParsePath(fullParsePath),
                isDirectory, Core.ModifiedAction.Changed));
        }

        public void NotifyDeleted(string fullParsePath, bool isDirectory)
        {
            dispatchEvent(CofeSystemEventArgs.FromParsePath(checkFullParsePath(fullParsePath),
                isDirectory, Core.ModifiedAction.Deleted));
        }

        public void NotifyRenamed(string fullParsePath, string oldParsePath, bool isDirectory)
        {
            dispatchEvent(CofeRenameEventArgs.FromParsePath(checkFullParsePath(oldParsePath), checkFullParsePath(fullParsePath),
                isDirectory, Core.ModifiedAction.Created));
        }

        #endregion

        #region Data

        string _volumeId;


        #endregion

        #region Public Properties

        #endregion
    }
}
